package org.real.guetshop.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.exception.StorageNotEnoughException;
import org.real.guetshop.pojo.dto.EditProductDTO;
import org.real.guetshop.pojo.dto.ShoppingCartDTO;
import org.real.guetshop.pojo.entity.ShopCart;
import org.real.guetshop.result.Result;
import org.real.guetshop.service.ShopCartService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/cart")
public class ShopCartController {

    @Resource
    private ShopCartService shopCartService;

    /**
     * 购物车添加物品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/addItem")
    @Operation(summary = "购物车添加物品")
    public Result addItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车山，商品信息为：{}",shoppingCartDTO);
        shopCartService.addShopCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/show")
    @Operation(summary = "查看购物车")
    public Result<List<ShopCart>> list(){
        List<ShopCart> list =shopCartService.showShopCart();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "清空购物车")
    public Result delete(){
        shopCartService.clean();
        return Result.success();
    }

    /**
     * 修改商品数量
     * @param editProductDTO
     * @return
     */
    @PutMapping("/editNum")
    @Operation(summary = "修改商品数量")
    public Result editNum(@RequestBody EditProductDTO editProductDTO) throws StorageNotEnoughException {
        log.info("减少购物车单个商品{}",editProductDTO);
        shopCartService.editNum(editProductDTO);
        return Result.success();
    }
}
